<?php

use Illuminate\Database\Seeder;

class MenuSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return mixed
     */
    public function run()
    {
        return $this->insert([
            [
                'menu_name' => '系统管理',
                'uri' => '',
                'icon' => 'el-icon-setting',
                'children' => [
                    [
                        'menu_name' => '员工设置',
                        'uri' => '/manager/user/paginate',
                        'icon' => 'el-icon-user'
                    ],
                    [
                        'menu_name' => '菜单管理',
                        'uri' => '/manager/menu/paginate',
                        'icon' => 'el-icon-menu'
                    ],
                    [
                        'menu_name' => '模板管理',
                        'uri' => '/manager/print/tpl/paginate',
                        'icon' => 'el-icon-printer'
                    ],
                    [
                        'menu_name' => '权限分组',
                        'uri' => '/manager/group/paginate',
                        'icon' => 'el-icon-open'
                    ],
                    [
                        'menu_name' => '密钥配置',
                        'uri' => '/manager/config/get',
                        'icon' => 'el-icon-key',
                        'children' => [
                            [
                                'menu_name' => '微信公众号',
                                'uri' => '/manager/config/wechat/gzh',
                                'icon' => 'el-icon-lock',
                            ]
                        ]
                    ]
                ]
            ],
            [
                'menu_name' => '投标管理',
                'uri' => '',
                'children' => [
                    [
                        'menu_name' => '用户管理',
                        'uri' => '/manager/user/manage',
                        'icon' => 'el-icon-user'
                    ],
                    [
                        'menu_name' => '标品管理',
                        'uri' => '/manager/goods/paginate',
                        'icon' => 'el-icon-files',
                        'children' => [
                            [
                                'menu_name' => '标品列表',
                                'uri' => '/manager/goods/paginate',
                                'icon' => 'el-icon-files'
                            ],
                            [
                                'menu_name' => '导表入库',
                                'uri' => '/manager/goods/import',
                                'icon' => 'el-icon-document-add'
                            ],
                            [
                                'menu_name' => '导表修改',
                                'uri' => '/manager/goods/importedit',
                                'icon' => 'el-icon-document-add'
                            ]
                        ]
                    ],
                    [
                        'menu_name' => '投标管理',
                        'uri' => '/manager/bid/paginate',
                        'icon' => 'el-icon-paperclip',
                        'children' => [
                            [
                                'menu_name' => '创建投标场次',
                                'uri' => '/manager/bid/paginate',
                                'icon' => 'el-icon-paperclip'
                            ],
                            [
                                'menu_name' => '标品出价查询',
                                'uri' => '/manager/bid/summary',
                                'icon' => 'el-icon-paperclip'
                            ],
                            [
                                'menu_name' => '标品出价详情',
                                'uri' => '/manager/bid/goods',
                                'icon' => 'el-icon-paperclip'
                            ]
                        ]
                    ],
                    [
                        'menu_name' => '货主管理',
                        'uri' => '/manager/supplier/paginate',
                        'icon' => 'el-icon-user'
                    ]
                ]
            ]
        ]);
    }

    /**
     * 插入数据
     *
     * @param array $menus
     * @param int $pid
     * @return bool
     */
    public function insert(array $menus, int $pid = 0) {
        foreach ($menus as $menu) {
            $data = [
                'menu_name' => $menu['menu_name'],
                'pid' => $pid,
                'icon' => isset($menu['icon']) ? $menu['icon'] : null,
                'is_hide' => isset($menu['is_hide']) ? $menu['is_hide'] : 0,
                'sort' => isset($menu['sort']) ? (int) $menu['sort'] : 50,
                'status' => isset($menu['status']) ? (int) $menu['status'] : 1,
                'created_at' => date('Y-m-d H:i:s')
            ];
            if (isset($menu['uri'])) {
                $data['uri'] = strtolower($menu['uri']);
            }
            $menu_id = DB::table('menus')->insertGetId($data);
            if (isset($menu['children']) && count($menu['children']) > 0) {
                $this->insert($menu['children'], $menu_id);
            }
        }
        return true;
    }
}
